package com.wy.controller;

import com.wy.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@CrossOrigin("*")
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;

    @PostMapping("/one")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        String handle = fileService.handle(multipartFile);
        System.out.println(handle);
        return handle;
    }
    @PostMapping("/more")
    @ResponseBody
    public String file1_1(@RequestParam("files") MultipartFile[] multipartFiles) throws IOException {
        if (multipartFiles != null && multipartFiles.length > 0) {
            for (MultipartFile file : multipartFiles) {
                if (!file.isEmpty()) {
                    System.out.println(file.getOriginalFilename());
                }
            }
            return "111";
        } else {
            return "222";
        }
    }

    // 下载到本地
    @GetMapping("/download")
    public void download(@RequestParam("fileName") String fileName, javax.servlet.http.HttpServletResponse response) throws java.io.IOException {
        fileService.downloadToLocal(fileName, response);
    }

    // 页面预览
    @GetMapping("/preview")
    public void preview(@RequestParam("fileName") String fileName, javax.servlet.http.HttpServletResponse response) throws java.io.IOException {
        fileService.previewFile(fileName, response);
    }
}
